<?php
namespace Api\Controller;

use Common\Controller\AppframeController;

class EngineController extends AppframeController {

	protected $i;
	protected $k;
	protected $ip;
	public function _initialize() {
		parent::_initialize();
		$this->ip = randIP();
		$this->k = !empty($_GET['kw']) ? urlencode($_GET['kw']) : '男科医院';
		session_start();
		$_SESSION['key'] = $this->k;
		header("Access-Control-Allow-Origin: *");
	}

	//百度mob
	public function bd_mob(){
		header("Access-Control-Allow-Origin: *");
		$i = $_GET['pn'] ? $_GET['pn'] : 0;
		$curl = new \Curl;
		$agent = $_SERVER['HTTP_USER_AGENT'];

		$headers = array();
		$headers[] = 'X-Apple-Tz: 0';
		$headers[] = 'X-Apple-Store-Front: 143444,12';
		$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
		$headers[] = 'Cache-Control: no-cache';
		$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
		$headers[] = "Host: m.baidu.com";
		$headers[] = "Origin: https://m.baidu.com";
		$headers[] = "Referer: https://m.baidu.com";

		$url = "http://m.baidu.com/s?word={$_SESSION['key']}&pn={$i}";
		$info = $curl->get($url,$agent,$headers);
//		$info = preg_replace('/<div class="ec_wise_ad">.*?<\/p>(\t|\r\n|\n|\s)*<\/div>/is'," ",$info);
		$info = remove_script($info);
		$info = str_replace("class=\"ec_wise_ad","style='display:none;' class=\"ec_wise_ad",$info);
		echo $info;
	}
	//百度pc
	public function bd_pc(){
		$i = $_GET['pn'] ? $_GET['pn'] : 0;
		$curl = new \Curl;
		$agent = get_rand_useragent();
		$header = $this->ip;
		$url = "http://www.baidu.com/s?wd={$_SESSION['key']}&pn={$i}";
		$info = $curl->get($url,$agent);
		$info = cdomain($info);
		$info = remove_script($info);
//		$info = str_replace("href=\"/s","href=\"http://www.baidu.com/s",$info);
		echo $info;
	}
	//360pc
	public function so_pc(){
		$i = $_GET['pn'] ? $_GET['pn'] : 0;
		$curl = new \Curl;
		$agent = $_SERVER['HTTP_USER_AGENT'];
		$url = "https://www.so.com/s?q={$_SESSION['key']}&pn={$i}";
		$info = $curl->get($url,$agent);
		$info = remove_script($info);
//		$info = str_replace("href=\"/s","href=\"http://www.so.com/s",$info);
		echo $info;
	}
	//360_mob
	public function so_mob(){
		$i = $_GET['pn'] ? $_GET['pn'] : 0;
		$curl = new \Curl;
		$agent = $_SERVER['HTTP_USER_AGENT'];
		$header = $this->ip;
		$url = "https://m.so.com/index.php?q={$_SESSION['key']}&pn={$i}";
		$info = $curl->get($url,$agent);
		$info = remove_script($info);
		$info = str_replace("class=\"e_idea_list","style='display:none;' class=\"e_idea_list",$info);
		$info = str_replace("href=\"/s","_href=\"http://m.so.com/s",$info);
		echo $info;
	}
	//搜狗pc
	public function sg_pc(){
		$i = $_GET['pn'] ? $_GET['pn'] : 0;
		$curl = new \Curl;
		$agent = get_rand_useragent();
		$header = $this->ip;
		$sugtime = time();
		$_asf = 'www.sogou.com';
		$url = "https://www.sogou.com/web?query={$_SESSION['key']}&page={$i}&_asf={$_asf}&sugtime={$sugtime}&ie=utf8";
		$info = $curl->get($url,$agent);
		$info = remove_script($info);
//		$info=preg_replace("/<(\/?link.*?)>/si","",$info); //过滤link标签
		$info = str_replace("href=\"?","rel=\"external\" _href=\"http://www.sogou.com/web?",$info);//相关
		$info = str_replace("href=\"/link","href=\"http://www.sogou.com/link",$info);
//		$info = str_replace("target=\"_blank\"","rel=\"external\"",$info);
		echo $info;
	}
	//搜狗_mob
	public function sg_mob(){
		$i = $_GET['pn'] ? $_GET['pn'] : 0;
		$sugtime = time();
		$curl = new \Curl;
		$agent = mob_agent();
		$header = $this->ip;
		$url = "https://m.sogou.com/web/searchList.jsp?t={$sugtime}&s_t={$sugtime}&s_from=index&keyword={$_SESSION['key']}&pg=webSearchList&p={$i}";
		$info = $curl->get($url,$agent);
		$info = remove_script($info);
		$info = str_replace(array('src="/','href="/'),array('src="http://m.sogou.com/','href="http://m.sogou.com/'),$info);
		echo $info;
	}



}